package cn.zs.supply.common.core.domain.result;

import cn.zs.supply.common.constants.enums.ErrorCodeEnum;
import cn.zs.supply.common.constants.enums.PubErrCode;
import cn.zs.supply.common.core.domain.vo.BaseVo;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
public class ResultBuilder {

    public static boolean isError(BaseResult result) {
        return !isSuccess(result);
    }

    public static boolean isSuccess(BaseResult result) {
        return PubErrCode.SUCCESS.getCode().equals(result.getErrcode());
    }

    public static BaseResult genSuccessBaseResult() {
        return genBaseResult(PubErrCode.SUCCESS.getCode(), PubErrCode.SUCCESS.getMsg());
    }

    public static BaseResult genBaseResult(ErrorCodeEnum errorCode) {
        return genBaseResult(errorCode.getCode(), errorCode.getMsg());
    }

    public static BaseResult genBaseResult(String retCode, String retMsg) {
        BaseResult result = new BaseResult();
        result.setSuccess(PubErrCode.SUCCESS.getCode().equals(retCode) ? true : false);
        result.setErrcode(retCode);
        result.setErrmsg(retMsg);
        return result;
    }


    public static <T extends BaseVo> ObjectResult<T> genSuccessObjectResult(T data) {
        ObjectResult<T> result = new ObjectResult<T>();
        result.setSuccess(true);
        result.setResult(data);
        result.setErrcode(PubErrCode.SUCCESS.getCode());
        result.setErrmsg(PubErrCode.SUCCESS.getMsg());
        return result;
    }

    public static <T extends Serializable> SerializableResult<T> genSuccessSerializableResult(T data) {
        SerializableResult<T> result = new SerializableResult<T>();
        result.setSuccess(true);
        result.setResult(data);
        result.setErrcode(PubErrCode.SUCCESS.getCode());
        result.setErrmsg(PubErrCode.SUCCESS.getMsg());
        return result;
    }

    public static <T extends Serializable> SerializableResult<T> genErrorSerializableResultForReturn(T data, String retCode, String retMsg) {
        SerializableResult<T> result = new SerializableResult<T>();
        result.setSuccess(false);
        result.setResult(data);
        result.setErrcode(retCode);
        result.setErrmsg(retMsg);
        return result;
    }

    public static <T extends BaseVo> ObjectResult<T> genErrorObjectResult(Class<T> viewCls, ErrorCodeEnum errorCode) {
        return genErrorObjectResult(viewCls, errorCode.getCode(), errorCode.getMsg());
    }

    public static <T extends BaseVo> ObjectResult<T> genErrorObjectResult(Class<T> viewCls, String retCode, String retMsg) {
        ObjectResult<T> result = new ObjectResult<T>();
        result.setSuccess(false);
        result.setErrcode(retCode);
        result.setErrmsg(retMsg);
        return result;
    }

    public static <T extends Serializable> SerializableResult<T> genErrorSerializableResult(Class<T> viewCls, ErrorCodeEnum errorCode) {
        return genErrorSerializableResult(viewCls, errorCode.getCode(), errorCode.getMsg());
    }

    public static <T extends Serializable> SerializableResult<T> genErrorSerializableResult(Class<T> viewCls, String retCode, String retMsg) {
        SerializableResult<T> result = new SerializableResult<T>();
        result.setSuccess(false);
        result.setErrcode(retCode);
        result.setErrmsg(retMsg);
        return result;
    }

    public static <T extends Serializable> SerializableResult<T> genErrorSerializableResultForReturn(Class<T> viewCls, String retCode, String retMsg) {
        SerializableResult<T> result = new SerializableResult<T>();
        result.setSuccess(true);
        result.setErrcode(retCode);
        result.setErrmsg(retMsg);
        result.setResult((T) viewCls);
        return result;
    }

    public static <T extends BaseVo> ListResult<T> genSuccessListResult(Collection<T> data) {
        ListResult<T> result = new ListResult<T>();
        result.setSuccess(true);
        result.setResult(data);
        result.setErrcode(PubErrCode.SUCCESS.getCode());
        result.setErrmsg(PubErrCode.SUCCESS.getMsg());
        return result;
    }

    public static <T, J> MapResult<T, J> genSuccessMapResult(Map<T, J> map) {
        MapResult<T, J> result = new MapResult<T, J>();
        result.setSuccess(true);
        result.setResult(map);
        result.setErrcode(PubErrCode.SUCCESS.getCode());
        result.setErrmsg(PubErrCode.SUCCESS.getMsg());
        return result;
    }

    public static <T extends BaseVo> ListResult<T> genErrorListResult(Class<T> viewCls, ErrorCodeEnum errorCode) {
        return genErrorListResult(viewCls, errorCode.getCode(), errorCode.getMsg());
    }

    public static <T, J> MapResult<T, J> genErrorMapResult(Class<T> tClass, Class<J> jClass, ErrorCodeEnum errorCode) {
        return genErrorMapResult(tClass, jClass, errorCode.getCode(), errorCode.getMsg());
    }

    public static <T extends BaseVo> ListResult<T> genErrorListResult(Class<T> viewCls, String retCode, String retMsg) {
        ListResult<T> result = new ListResult<T>();
        result.setSuccess(false);
        result.setErrcode(retCode);
        result.setErrmsg(retMsg);
        return result;
    }

    public static <T, J> MapResult<T, J> genErrorMapResult(Class<T> tClass, Class<J> jClass, String retCode, String retMsg) {
        MapResult<T, J> result = new MapResult<>();
        result.setSuccess(false);
        result.setErrcode(retCode);
        result.setErrmsg(retMsg);
        return result;
    }

    public static <T extends BaseVo> PageResult<T> genSuccessPageResult(Collection<T> data, Long total, Integer pageSize, Integer pageNum) {
        PageResult<T> result = new PageResult<T>();
        result.setSuccess(true);
        result.setErrcode(PubErrCode.SUCCESS.getCode());
        result.setErrmsg(PubErrCode.SUCCESS.getMsg());
        result.setResult(data);
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setTotal(total);
        return result;
    }
    
    public static <T extends BaseVo> PageResult<T> genSuccessPageResult(Collection<T> data, Long total, Integer pageSize, Integer pageNum,
                                                                        Integer pages,Integer startRow) {
        PageResult<T> result = new PageResult<T>();
        result.setSuccess(true);
        result.setErrcode(PubErrCode.SUCCESS.getCode());
        result.setErrmsg(PubErrCode.SUCCESS.getMsg());
        result.setResult(data);
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setTotal(total);
        result.setPages(pages);
        result.setStartRow(startRow);
        return result;
    }

    public static <T extends BaseVo> PageResult<T> genErrorPageResult(Class<T> viewCls, ErrorCodeEnum errorCode) {
        return genErrorPageResult(viewCls, errorCode.getCode(), errorCode.getMsg());
    }

    public static <T extends BaseVo> PageResult<T> genErrorPageResult(Class<T> viewCls, String retCode, String retMsg) {
        PageResult<T> result = new PageResult<T>();
        result.setSuccess(false);
        result.setErrcode(retCode);
        result.setErrmsg(retMsg);
        return result;
    }

    public static <T extends BaseVo> WaterFallPageResult<T> genSuccessWaterFallPageResult(Collection<T> data, Long nextOffset, Date requestTime) {
        WaterFallPageResult<T> result = new WaterFallPageResult<>();
        result.setSuccess(true);
        result.setErrcode(PubErrCode.SUCCESS.getCode());
        result.setErrmsg(PubErrCode.SUCCESS.getMsg());
        result.setResult(data);
        result.setNextOffset(nextOffset);
        result.setNextTime(requestTime);
        return result;
    }

    public static <T extends BaseVo> WaterFallPageResult<T> genErrorWaterFallPageResult(Class<T> viewCls, ErrorCodeEnum errorCode) {
        return genErrorWaterFallPageResult(viewCls, errorCode.getCode(), errorCode.getMsg());
    }

    public static <T extends BaseVo> WaterFallPageResult<T> genErrorWaterFallPageResult(Class<T> viewCls, String retCode, String retMsg) {
        WaterFallPageResult<T> result = new WaterFallPageResult<>();
        result.setSuccess(false);
        result.setErrcode(retCode);
        result.setErrmsg(retMsg);
        return result;
    }
}

